// JavaScript Document

//var site_url = "http://www.sennell.com.au:8111/ant/";
var site_url = "http://localhost/ant/";

function site_seach(srh)
{
	redirect(site_url+"search/seach_global/"+srh);	
}
function togEle(ele)
{
	$("#"+ele).toggle(3);	
}

function openw(path, s)
{
	var set ="resizable=yes,titlebar=yes,location=no";
	set = set+s;
	window.open(path,"newWin",set);
}

function showMainMenu(t)
{
	$("div[id^=div_nav_main_]").hide();
	console.log($(t).next().show());


}
function showJobDes(id)
{
	
	var display = ( $("#jobId_"+id).css("display"));
	if(display == "none")
	{
		$("#showJobDecImg_"+id ).html("-")
	}
	else{
		$("#showJobDecImg_"+id ).html("+")
	}
	$("#jobId_"+id	).toggle();
}


function gotoJob(id)
{
	var loc = site_url+"job/editjob/"+id;
	window.location = loc;	
}

function gotoClient(id)
{
	var loc = site_url+"client/editclient/"+id;
	window.location = loc;	
}

function gotoUser(id)
{
	var loc = site_url+"user/edituser/"+id;
	window.location = loc;	
}


function redirect(url)
{
	document.location.href=url;	
}

function ajax_loader(i)
{
//	alert(i);
	if(i == '1')
	{
		$("#ajax_loader").show();
	}
	else{
		$("#ajax_loader").hide();
	}
}


function worklog_job()
{
	
	ajax_loader('1');
	var client_id = ($(this).val());
	
	if(client_id == 0)
	{
		
		$("#worklog_job").hide();
	} 
	else{
		
		var out = "";
		$.post(site_url + "ajaxcall/worklog_getjobs/", {company_id: client_id},
	            function(data){
					if(data != "")
					{
						$("#worklog_job").show().html(data);	
						$("#nojobs").hide();
					}
					else{
						$("#nojobs").show();
						$("#worklog_job").hide();
					}
					ajax_loader('0');
	            }, 'json');
	}

}
$(function() {
	
	//================== STYLEING
	
	var body_height = $(window).height();
	$("#navWrap").css('height' , $("#footer").prev().height()+'px');
	$("#navWrap").parent().css('width' , '150px');
	//$("#footer").prev().css("margin-bottom", '20px');
	
	$(".page_login .speelContainer").remove();
	
	//=====================================
	$(".qt .tbl_row").qtip(
	{
	   content: "Tooltip",
		position: {
		   target: 'mouse',
		   adjust: { mouse: true }
		},
		show: { delay: 0 },
	   show: 'mouseover',
	   hide: 'mouseout'
	});
		//$( "#accordion" ).accordion({ active: 1});
		 $('#identifier').hoverAccordion(
		 {
		
       	 onClickOnly : true  
		 }); 
	
		$( "#job_start_date" ).datepicker();
		$( "#job_start_date" ).datepicker( "option", "dateFormat", "yy-mm-dd " );
		
			/*
			 * 
			 */
			var cdate = $("#job_start_date" ).attr("data");
			//console.log("date=> " + (cdate));
			if(cdate != "")
			{
				$( "#job_start_date" ).datepicker('setDate', new Date(cdate) );
			}
			else{
				$( "#job_start_date" ).datepicker('setDate', new Date());
			}

		
		$( "#job_start_date" ).change(function()
		{
			$( "#job_start_date_h" ).val($( "#job_start_date" ).val().substr(3));
		});
		
		$( "#job_end_date" ).datepicker();
		$( "#job_end_date" ).datepicker( "option", "dateFormat", "yy-mm-dd " );
			
		var edate = $("#job_end_date" ).attr("data");
			//console.log("date=> " + (edate));
			if(cdate != "")
			{
				$( "#job_end_date" ).datepicker('setDate', new Date(edate) );
			}
			else{
				$( "#job_end_date" ).datepicker('setDate', new Date());
			}
			
		$( "#job_end_date" ).change(function()
		{
			$( "#job_end_date_h" ).val($( "#job_end_date" ).val().substr(3));
		});
		

		$( "#report_start" ).datepicker();
		$( "#report_start" ).datepicker( "option", "dateFormat", "yy-mm-dd " );
		$( "#report_start" ).datepicker('setDate', new Date());

		
		$( "#report_end" ).datepicker();
		$( "#report_end" ).datepicker( "option", "dateFormat", "yy-mm-dd " );
		$( "#report_end" ).datepicker('setDate', new Date());

		
	
	$(".tblBody").hover(function()
	{
		//console.log("sada");
		$(this).toggleClass('trHoverGray');
	});
	
	$("#worklog_company").change(worklog_job)
	
	/*
	 * 
	 */
	$(".worklog_action").change(function(){
		var v =  $(this).val();
		var vals = v.split(",");
		
		console.log(vals[0] );
		if(vals[0] == 1)
		{
			redirect(site_url+"worklog/editworklog/"+vals[1]);
		}
		if(vals[0] == 3)
		{
			redirect(site_url+"job/editjob/"+vals[1]);
		}
	});
	
	
	/*
	 * 
	 */
	$(".client_act_delete").click(function(){
		var jobId =  $(this).attr("jobID");
		

		if(jobId != "")
		{
			//redirect(site_url+"job/editjob/"+vals[1]);
			var del_cleint = confirm("Delete Client?")
			
			if(del_cleint)
			{
				var out = "";
				$.post(site_url + "ajaxcall/client_delete/", {id: jobId},
			            function(data){
							if(true)
							{
								redirect(site_url+"client/showallclients");
							}
							else{
								alert("Unable to delete this client!")
							}
			            }, 'json');
				
			}
		}
		
	});

	
	/*
	 * 
	 */
	$(".job_action").change(function(){
		var v =  $(this).val();
		var vals = v.split(",");
		
		
		if(vals[0] == 1)
		{
			redirect(site_url+"job/editjob/"+vals[1]);
		}
		if(vals[0] == 3)
		{
			//redirect(site_url+"job/editjob/"+vals[1]);
			var del_cleint = confirm("Delete Job?")
			
			if(del_cleint)
			{
				var out = "";
				$.post(site_url + "ajaxcall/job_delete/", {id: vals[1]},
			            function(data){
							if(true)
							{
								redirect(site_url+"job/showalljobs");
							}
							else{
								alert("Unable to delete this job!")
							}
			            }, 'json');
				
			}
		}
	});
	
	
	$(".user_act_delete").click(function(){
		var v =  $(this).attr("data");
		

		if(v != "")
		{
			//redirect(site_url+"job/editjob/"+vals[1]);
			var del_cleint = confirm("Delete User?")
			
			if(del_cleint)
			{
				var out = "";
				$.post(site_url + "ajaxcall/user_delete/", {id: v},
			            function(data){
							if(true)
							{
								redirect(site_url+"user/showallusers");
							}
							else{
								alert("Unable to delete this user!")
							}
			            }, 'json');
				
			}
		}		
		
	})
	
	
	$(".job_invoiced").change(function(){
		
		var id = $(this).attr("data");
		var st = $(this).is(":checked");
		///console.log(id);
		if(st)
		{
			var st1 = "1";
		}
		else
			{
			var st1 = "0";
			}
			$.post(site_url + "ajaxcall/job_invoiced/", {id: id, status:st1},
		            function(data){
						$("#reportNoti").fadeIn().fadeOut();
		            }, 'json');
	})
	
	$("#clientViewShowAll").click(function(){
		
		redirect(site_url+"clientview/all");
	});
	
	$("#user_type").change(function(){
		
		var type = $(this).val();
		if(type == 3)
		{
			$("#company").show();
		}
		else{
			$("#company").hide();
		}
	})
	
	$("#form_newJob").submit(function()
	{
		var c_id = $( "#company_id").val();
		if( c_id == "0")
		{
			$(".pageMsg").html("*You have not selected a client").show();	
			return false;
		}
		else if( c_id= "-1")
		{
			if(	$("#client_name").val() == "")
			{
				$(".pageMsg").html("*You have not selected a client").show();	
				return false;
			}
		}
		
	});
	
	$("#company_id").change(function()
	{
		if($(this).val() == -1)
		{
			var cod_html ="<input name='cod_client_name' type='text' id='client_name' size='43' />";
			$("#cod_client").html(cod_html);
		}
		else{
			$("#client_name").remove();
		}
	});
	
	
	$("#site_search_btn").click(function()
	{
		var srh = $("#site_search_txt").val();
		srh = srh.replace("/",",");
		site_seach(srh);
	})
	
	$("#site_search_txt").keydown(function(event)
	{
	
		if (event.keyCode == '13')
		{
			var srh = $("#site_search_txt").val();
			srh = srh.replace("/",",");
			site_seach(srh);
		}
	})
	
	$("#client_job_history").change(function()
	{
		var v = $(this).val();
		
		if(v != '0')
		{
			var url = site_url+ "report/job_jobs/" +v;
			//redirect(site_url+ "report/job_jobs/" +v);
			var win_client_job_history = window.open(url,"win_client_job_history");
			win_client_job_history.focus();
			
		}
	})
	
	
	
	
	//=========
	$("#btnLogout").click(function(){
		redirect(site_url+ "user/logout/");
	})
	
}) ;